package tako.wisers.com.android.apis.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.util.Global;

public class DialogManager {
	private Context mCtx;
	private Dialog mDialog;
	private int id;
	
	
	public DialogManager(Context context){
		this.mCtx = context;
	}
	
	public Dialog build(int id){
		if(mDialog != null && mDialog.isShowing()){
			mDialog.dismiss();
		}
		this.id = id;
		switch (id) {
		case Global.EXIT_DIALOG:
			mDialog = exitDialogInit();
			mDialog.setTitle(R.string.exit_dialog_msg);
			break;
		case Global.READ_DIALOG:
			mDialog = ProgressDialog.show(mCtx, "",
					mCtx.getText(R.string.read_dialog_msg), true, false);
			break;
		case Global.RESTART_DIALOG:
			mDialog = exitDialogInit();
			mDialog.setTitle(R.string.restart_dialog_msg);
			break;
		case Global.WAITING_DIALOG:
			mDialog = waitingDialogInit();
			mDialog.setTitle(R.string.search_failed);
			break;
		default:
			break;
		}
		return mDialog;
	}
	
	private Dialog waitingDialogInit() {
		Dialog tempDialog = new AlertDialog.Builder(mCtx)
		.setIcon(R.drawable.alert_dialog_icon).setNegativeButton("OK",  new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				mDialog.dismiss();
			}
		}).create();
		return tempDialog;
	}

	public int getId() {
		return id;
	}

	private Dialog exitDialogInit() {
		Dialog tempDialog = new AlertDialog.Builder(mCtx)
		.setIcon(R.drawable.alert_dialog_icon)
		.setPositiveButton(R.string.dialog_button_yes, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int whichButton) {
		    	mDialog.dismiss();
				android.os.Process.killProcess(android.os.Process.myPid());
		    }
		})
		.setNegativeButton(R.string.dialog_button_no, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int whichButton) {
		    	mDialog.cancel();
		    }
		}).create();
		return tempDialog;
		
	}
	
	public void dismiss(){
		if(mDialog != null){
			mDialog.dismiss();
		}
	}
}
